local events = {}
local handlers = {}

local MANA_REGEN_UPDATE_RATE = 2.0

function Spy.CreateStatFrame()
   local counter = 0.0
   local realMP5 = 0
   local oldMana = 0

   local f = CreateFrame("FRAME", "SpyStats", UIParent)
   f:SetWidth(200)
   f:SetHeight(80)

   function OnUpdate(self, elapsed)
      counter = counter + elapsed
      if counter >= MANA_REGEN_UPDATE_RATE then
         local curMana = UnitMana("player")
         realMP5 = (curMana - oldMana) * (5.0 / counter)
         oldMana = curMana
         counter = 0.0
         local mp5, mp5Combat = GetManaRegen()
         local text = format("MP5: %.1f | %.1f (%.1f)", mp5Combat*5, mp5*5, realMP5)
         self.statMP5:SetText(text)
      end
   end
   
   --f:SetPoint("BOTTOMLEFT", SpyManaBar)
   f:SetScript("OnUpdate", OnUpdate)
   f:SetFrameLevel(SpyManaBarV:GetFrameLevel() + 10)
   f.statMP5 = f:CreateFontString("$parent_MP5_text", "OVERLAY", "NumberFontNormalSmall")
   f.statMP5:SetAllPoints()
   f.statMP5:SetAlpha(1.0)
   f.statMP5:SetTextColor(1, 1, 1)
   f.statMP5:SetText("MP5")
   f.statMP5:SetJustifyH("LEFT")
   f.statMP5:Show()
   f:Show()
end

-------------------------------------------------------------------------------
function events:UNIT_MAXMANA(unit)
   if unit == "player" then
      self:Init(0, UnitManaMax("player"))
   end
end

function events:UNIT_MANA(unit)
end

function events:PLAYER_ENTERING_WORLD()
   self:Init(0, UnitManaMax("player"))
end

function events:PLAYER_LOGIN()
   self:Init(0, UnitManaMax("player"))
end

function events:UNIT_SPELLCAST_START(unit, spell, rank)
   if unit == "player" then
      local _, _, _, cost = GetSpellInfo(spell)
      if cost then
         self.predictOffset = -cost
         self.bUpdate = true
      end
   end
end

function events:UNIT_SPELLCAST_SENT(unit, spell, rank, target)
   events.UNIT_SPELLCAST_START(self, unit, spell, rank)
end

-------------------------------------------------------------------------------
function events:UNIT_SPELLCAST_STOP(unit, spell, rank)
   if unit == "player" then
      self:SetPredict(0)
   end
end

function events:UNIT_SPELLCAST_SUCCEEDED(...)
   events.UNIT_SPELLCAST_STOP(self, ...)
end
function events:UNIT_SPELLCAST_INTERRUPTED(...)
   events.UNIT_SPELLCAST_STOP(self, ...)
end
function events:UNIT_SPELLCAST_FAILED(...)
   events.UNIT_SPELLCAST_STOP(self, ...)
end
function events:UNIT_SPELLCAST_FAILED_QUIET(...)
   events.UNIT_SPELLCAST_STOP(self, ...)
end

-------------------------------------------------------------------------------
local function InitMana()
   function GetMana(self)
      return UnitMana("player")
   end
   
   local f, bar
   f = CreateFrame("FRAME", "SpyMana", UIParent)
   f:SetAllPoints()

   bar = Spy.CreateLayeredBar("SpyManaBarV", f, true)
   bar:SetHeight(400)
   bar:SetWidth(SpyDB.HealthBar.Height)
   bar:SetFrameLevel(5)
   bar:SetPoint("TOPLEFT", 0, -100)
   bar.GetValueFunc = GetMana
   bar.GetUpdatedText = function(self)
      local min, max = self.bar:GetMinMaxValues()
      local pct = (self.bar:GetValue() / max) * 100.0
      if pct < 100 then
         return format("%d", pct)
      else
         return ""
      end
   end
   Spy.RegisterAll(bar, events, handlers)
   Spy.CreateStatFrame()
end

-- Spy.AddStartupFunc(InitMana)

